<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
		
	);
	$list=$this->get_list('game',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['issaonum']=$this->pdo_count('sao_log',array('uniacid'=>$_SESSION['uniacid'],'game_id'=>$item['id']),'id');
			$item['isupnum']=$this->pdo_count('video_log',array('uniacid'=>$_SESSION['uniacid'],'game_id'=>$item['id']),'id');
			$item['coupons']=$item['coupons']?json_decode($item['coupons'],true):array();
			$item['coupon_id']=$item['coupons'][0]['coupon_id'];
			//登录二维码
			$item['authurl']=custom_url('jy_douyintui/auth//id='.$item['id'],true);
			$nowlink=custom_url('jy_douyintui/game//id='.$item['id'],true);
			$redirect_uri=urlencode($nowlink);
			$scope='video.create,user_info,data.external.item,mobile_alert';
			$authurl='https://open.douyin.com/platform/oauth/connect?client_key='.$setting['client_key'].'&response_type=code&scope='.$scope.'&redirect_uri='.$redirect_uri;
			$item['tppauthurl']=$authurl;
			if($item['starttime']>TIMESTAMP){
				$item['status']=0;
			}elseif($item['endtime']<TIMESTAMP){
				$item['status']=2;
			}else{
				$item['status']=1;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动管理'),
			array('op'=>'edit','title'=>'添加活动'),
		),
		//'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：</br>1、需要4个接口支持：video.create,user_info,data.external.item,mobile_alert；2、抖音需要申请：data.external.item权限，才能获取用户的视频数据（点赞，分享，播放数等）;并且只有每次客户二次扫活动二维码的时候才能获取！','del_one'=>true,'del_message'=>'确定删除活动吗？删除推广出去的二维码都将失效！','delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('name'=>'type','title'=>'活动类型','type'=>'radio','options'=>'type'),
			array('name'=>'coupon_id','title'=>'优惠券','type'=>'select','options'=>'coupons'),
			array('name'=>'saonum','title'=>'限制扫次数','type'=>'string'),
			array('name'=>'isupnum','title'=>'已扫次数(成功同步视频)','type'=>'string'),
			array('name'=>'issaonum','title'=>'浏览次数','type'=>'string'),
			array('name'=>'authurl','title'=>'链接地址','type'=>'copy'),
			array('name'=>'authurl','title'=>'二维码','type'=>'ewm'),
			array('name'=>'tppauthurl','title'=>'抖音二维码','width'=>300,'type'=>'ewm'),
			array('name'=>'status','title'=>'活动状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'href','op'=>'copy','title'=>'复制活动'),
			array('type'=>'href','action'=>'/hexiao_user/','title'=>'核销员'),
			array('type'=>'modal','op'=>'tui','title'=>'推广'),
			array('type'=>'href','action'=>'/coupon_log/','title'=>'领券记录'),
			array('type'=>'href','action'=>'/sao_log/','title'=>'参与用户'),
			array('type'=>'modal','op'=>'count','title'=>'活动数据'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'活动名称','type'=>'string'),
			array('name'=>'type','width'=>15,'title'=>'活动类型','type'=>'radio','options'=>'type'),
			array('name'=>'status','width'=>15,'title'=>'活动名称','type'=>'radio','options'=>'status'),
		),
	);
	$return['coupons']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['type']=$this->game_type;
	$return['status']=$this->game_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='tui'){
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('game',array('id'=>$id));
	$item['authurl']=custom_url('jy_douyintui/auth//id='.$item['id'],true);
	$nowlink=custom_url('jy_douyintui/game//id='.$item['id'],true);
	$redirect_uri=urlencode($nowlink);
	//先注销data.external.item
	$scope='video.create,user_info,data.external.item,mobile_alert';
	$authurl='https://open.douyin.com/platform/oauth/connect?client_key='.$setting['client_key'].'&response_type=code&scope='.$scope.'&redirect_uri='.$redirect_uri;
	$item['tppauthurl']=$authurl;
	$item['poster_data']=$item['poster_data']?json_decode($item['poster_data'],true):array();
	$poster=array(
		'id'=>$item['id'],
		'data'=>$item['poster_data']['data'],
		'bg'=>$item['poster_data']['image'],
	);
	$item['posterimg']=$this->model('model')->get_poster($item['tppauthurl'],$poster);
	$htmls=array(
		'type'=>'look',
		'desc'=>'推广',
		'look'=>array(
			array('type'=>'ewm','title'=>'二维码','width'=>300,'name'=>'tppauthurl','read'=>true),
			array('type'=>'image','width'=>150,'title'=>'推广海报','name'=>'posterimg','read'=>true),
		)
	);
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='count'){
	$id=intval($_RQ['id']);
	$item['sao_num']=$this->pdo_count('sao_log',array('game_id'=>$id),'id');
	$item['coupon_num']=$this->pdo_count('coupon_log',array('game_id'=>$id),'id');
	$item['iscoupon_num']=0;
	$item['nocoupon_num']=$item['coupon_num']-$item['iscoupon_num'];
	$htmls=array(
		'type'=>'look',
		'desc'=>'活动数据',
		'look'=>array(
			array('type'=>'string','title'=>'参与人数','name'=>'sao_num','read'=>true),
			array('type'=>'string','title'=>'领券总数','name'=>'coupon_num','read'=>true),
			array('type'=>'string','title'=>'已使用券','name'=>'iscoupon_num','read'=>true),
			array('type'=>'string','title'=>'未使用券','name'=>'nocoupon_num','read'=>true),
		)
	);
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='copy'){
	$id=intval($_RQ['id']);
	exi('','','/game/edit/copyid='.$id);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	$copyid=intval($_RQ['copyid']);
	if($id){
		$item=$this->pdo_get('game',array('id'=>$id));
	}elseif($copyid){
		$item=$this->pdo_get('game',array('id'=>$copyid));
		unset($item['id']);
	}else{
		$item=array();
	}
	$item['diy_titles']=$item['diy_titles']?json_decode($item['diy_titles'],true):array();
	$item['video_datas']=$item['video_datas']?json_decode($item['video_datas'],true):array();
	$item['poster_data']=$item['poster_data']?json_decode($item['poster_data'],true):array('data'=>$this->poster_datademo,'image'=>'../addons/jy_douyintui/resource/images/poster_bg.jpg');
	$item['coupons']=$item['coupons']?json_decode($item['coupons'],true):array();
	$item['starttime'] =$item['starttime']?date('Y/m/d H:i',$item['starttime']):'';
	$item['endtime'] =$item['endtime']?date('Y/m/d H:i',$item['endtime']):'';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'活动设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动管理'),
			array('op'=>'edit','title'=>'添加活动'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','must'=>true,'title'=>'活动名称','type'=>'string'),
					//array('name'=>'logo','title'=>'活动logo','type'=>'image'),
					array('name'=>'merchid','title'=>'绑定商家','type'=>'choose','action'=>'get_merch','help'=>'不绑定商家，就表示是站点的活动！'),
					array('type'=>'time','title'=>'开始时间','name'=>'starttime'),
					array('type'=>'time','title'=>'结束时间','name'=>'endtime'),
					array('name'=>'type','title'=>'活动类型','type'=>'radio','options'=>'type'),
					array('name'=>'linktype','title'=>'跳转类型','type'=>'radio','options'=>'linktype','help'=>'领券(卡券模式)或发布视频(纯推广模式)后的跳转页面类型'),
					array('type'=>'string','title'=>'自定义链接','name'=>'linkurl','show'=>array('linktype'=>2)),
					array('name'=>'tpltype','title'=>'活动模板风格','type'=>'radio','options'=>'tpltype'),
					array('name'=>'saonum','title'=>'限制扫码次数(同步视频)','type'=>'string'),
					array('name'=>'douyinurl','title'=>'抖音商户主页','type'=>'string','help'=>'填入商户抖音门店的分享链接，可留空，例如：https://v.douyin.com/xxxx/'),
					array('name'=>'douyintitle','title'=>'抖音商户名称','type'=>'string','help'=>'填入抖音商户名称，视频的商户名称跟该选项有关'),
					array('name'=>'douyinaddress','title'=>'商户联系地址','type'=>'string','help'=>'填入商户地址，用于页面显示，与视频内容无关'),
					array('name'=>'douyinpathurl','title'=>'抖音商户位置','type'=>'string','help'=>'填入抖音商户地理位置页的分享链接，视频的地址跟该选项有关，可留空，例如：https://v.douyin.com/xxxxx/'),
					array('name'=>'app_id','title'=>'小程序id','type'=>'string','help'=>'示例：tt00000000dddd'),
					array('name'=>'app_url','title'=>'小程序url','type'=>'string','help'=>'吊起小程序时的url参数。示例：pages/index?param=123'),
					array('name'=>'app_title','title'=>'小程序标题','type'=>'string'),
					//array('name'=>'status','title'=>'活动状态','type'=>'radio','options'=>'status'),
			)),
			array(
					'title'=>'活动介绍',
					'list'=>array(
						array('type'=>'richtext','title'=>'活动介绍','name'=>'content'),
					)
			),
			array(
					'title'=>'页面配置',
					'list'=>array(
						array('type'=>'string','title'=>'《~恭喜~》替换','name'=>'diy_titles.gongxi','help'=>'替换页面“~恭喜~”文本'),
						array('type'=>'string','title'=>'《你已经成功参与传播达人》替换','name'=>'diy_titles.chenggong','help'=>'替换页面“你已经成功参与传播达人”文本'),
						array('type'=>'string','title'=>'《你参与传播的作品是》替换','name'=>'diy_titles.cangyu','help'=>'替换页面“你参与传播的作品是”文本'),
						array('type'=>'string','title'=>'《活动介绍》替换','name'=>'diy_titles.huodong','help'=>'替换页面“活动介绍”文本'),
						array('type'=>'string','title'=>'《立即领取卡券》替换','name'=>'diy_titles.ljlq','help'=>'替换页面“立即领取卡券”文本'),
						array('type'=>'string','title'=>'《查看我的卡券》替换','name'=>'diy_titles.ckwdkq','help'=>'替换页面“查看我的卡券”文本'),
						array('type'=>'string','title'=>'《领取失败》替换','name'=>'diy_titles.lqsb','help'=>'替换页面“领取失败”文本'),
					)
			),
			array(
					'title'=>'短视频',
					'list'=>array(
						array('type'=>'image','title'=>'视频封面','name'=>'video_logo'),
						array('type'=>'unitstring','unit'=>'秒','title'=>'延迟发布','name'=>'video_lasttime'),
						array('type'=>'array','title'=>'视频添加','name'=>'video_datas','add_tip'=>'添加一个视频',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'视频名称'),
									array('type'=>'string','name'=>'url','title'=>'视频地址'),									
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'视频名称'),
									array('type'=>'video','name'=>'url','title'=>'视频地址'),
								)
						),
					)
			),
			array(
					'title'=>'海报设置',
					'list'=>array(
						array('type'=>'poster','title'=>'活动海报','name'=>'poster_data','datatypes'=>'datatypes','datademo'=>'datademo','datapushs'=>'datapushs'),
					)
			),
			array(
					'title'=>'优惠券设置',
					'list'=>array(
						array('type'=>'string','name'=>'hexiaopass','title'=>'核销密码'),
						array('type'=>'array',
						'name'=>'coupons',
						'add_tip'=>'添加一个优惠券',
						'title'=>'优惠券列表',
								'list'=>array(
									array('type'=>'select','name'=>'coupon_id','title'=>'优惠券','options'=>'coupons'),	
								),
								'details'=>array(
									array('type'=>'select','name'=>'coupon_id','title'=>'优惠券','options'=>'coupons'),
								)
						),
					)
			),
			array(
					'title'=>'接口设置',
					'list'=>array(
						array('type'=>'checkbox','title'=>'用户管理','name'=>'api_userset','options'=>'api_userset'),
						array('type'=>'checkbox','title'=>'抖音视频','name'=>'api_videoset','options'=>'api_videoset'),
					)
			),
		)
	);
	$return['datatypes']=$this->poster_datatypes;
	$return['datademo']=array('data'=>$this->poster_datademo,'image'=>'../addons/jy_douyintui/resource/images/poster_bg.jpg');
	$return['datapushs']=$this->poster_datapushs;
	$return['coupons']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('title','id'),'id');
	$return['linktype']=$this->linktype;
	$return['tpltype']=$this->tpltype;
	$return['type']=$this->game_type;
	$return['status']=$this->game_status;
	$return['api_videoset']=$this->api_videoset;
	$return['api_userset']=$this->api_userset;
	$return['settype']='qr';
	$return['nowindex']=0;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['title']){
		exi('活动名称必填','error');
	}
	$data =array(
		'title'=>$post['title'],
		'starttime'=>strtotime($post['starttime']),
		'endtime'=>strtotime($post['endtime']),
		'type'=>intval($post['type']),
		'merchid'=>intval($post['merchid']),
		'linktype'=>intval($post['linktype']),
		'tpltype'=>intval($post['tpltype']),
		'linkurl'=>$post['linkurl'],
		'saonum'=>$post['saonum'],
		'douyinurl'=>$post['douyinurl'],
		'douyintitle'=>$post['douyintitle'],
		'douyinaddress'=>$post['douyinaddress'],
		'douyinpathurl'=>$post['douyinpathurl'],
		'app_id'=>$post['app_id'],
		'app_url'=>$post['app_url'],
		'app_title'=>$post['app_title'],
		'content'=>$post['content'],
		'diy_titles'=>ijson_encode($post['diy_titles']),
		'video_logo'=>$post['video_logo'],
		'video_lasttime'=>$post['video_lasttime'],
		'video_datas'=>ijson_encode($post['video_datas']),
		'poster_data'=>ijson_encode($post['poster_data']),
		'coupons'=>ijson_encode($post['coupons']),
		'hexiaopass'=>$post['hexiaopass'],
		'api_userset'=>$post['api_userset'],
		'api_videoset'=>$post['api_videoset'],
	);
	if($post['id']){
		$this->pdo_update('game',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		$this->pdo_insert('game',$data);
		exi('添加成功','','list');
	}
	
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择活动！！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_levelfee_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>